<template>
    <el-select v-model="model" placeholder="请选择省" style="width:100%" @change="change" >
        <el-option v-for="item in list" :key="item.provinceId" :label="item.provinceName" :value="item.provinceId">
        </el-option>
    </el-select>
</template>

<script>
import { queryAllProvince } from 'api/common';

export default {
    props: {
        value: {
            type: [String, Number],
            required: true
        },
        name: {
            type: String,
            default: ''
        },
        nochose:''
    },
    computed: {
        model: {
            get() {
                if (this.list) {
                    if (this.list.some(item => item.provinceId == this.value)) {
                        let provinceObj = this.list.find((item) => {
                            return item.provinceId === this.value;
                        });
                        // let name = provinceObj.provinceId ? provinceObj.provinceName : '';
                        // this.$emit('getProvinceName',name); 
                        return this.value;
                    } else {
                        this.$emit('input', '');
                        return '';
                    }
                } else {
                    return '';
                }
            },
            set(val) {
                this.$emit('input', val);
            }
        }
    },
    data() {
        return {
            list: null
        }
    },
    created() {
        this.getList();
    },
    methods: {
        getList() {
            queryAllProvince().then(res => {
                this.list = res.data.content;
                if(this.nochose){//不需添加請選擇
                    return false
                }else{
                    this.list.unshift({
                        provinceId: '',
                        provinceName: '请选择省'
                    });
                }
            });
        },
        change(val) {
            let provinceObj = this.list.find((item) => item.provinceId === val);
            if (provinceObj && provinceObj.provinceId) {
                this.$emit('update:name', provinceObj.provinceName);
            } else {
                this.$emit('update:name', '');
            }
            this.$emit('change', val);
        }
    }
}
</script>

<style>

</style>
